package com.intramirror.product.utils;

import com.intramirror.product.entity.AggregateRule;
import com.intramirror.product.entity.SpuBoutiqueImgSort;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Created on 2018/1/30.
 * @author 123
 */
public class AggregateRuleManagement {
    private static final Logger LOGGER = LoggerFactory.getLogger(AggregateRuleManagement.class);
    private static AggregateRuleManagement instance = new AggregateRuleManagement();
    private Map<Long, List<AggregateRule>> ruleMap = new HashMap<>();
    private Map<Long, Integer> boutiqueImgSortMap = new HashMap<>();
    private Boolean aggregateFlag = false;

    private AggregateRuleManagement() {

    }

    public static AggregateRuleManagement getInstance() {
        return instance;
    }

    public void setRuleList(List<AggregateRule> ruleList) {
        ruleMap.clear();
        for (AggregateRule rule : ruleList) {
            Long vendorId = rule.getVendorId();
            List<AggregateRule> listBrandRule;
            if (ruleMap.containsKey(vendorId)) {
                listBrandRule = ruleMap.get(vendorId);
            } else {
                listBrandRule = new ArrayList<>();
                ruleMap.put(vendorId, listBrandRule);
            }
            listBrandRule.add(rule);
        }
    }

    public void setBoutiqueImgSort(List<SpuBoutiqueImgSort> boutiqueImgSortList) {
        for (SpuBoutiqueImgSort boutiqueImgSort : boutiqueImgSortList) {
            Long vendorId = boutiqueImgSort.getVendorId();
            Integer sort = boutiqueImgSort.getSort();
            boutiqueImgSortMap.put(vendorId, sort);
        }
    }

    public AggregateRule getRule(Long vendorId, Long brandId) {
        List<AggregateRule> listBrandRule = ruleMap.get(vendorId);
        if (listBrandRule != null) {
            for (AggregateRule rule : listBrandRule) {
                if (rule.getBrandId() == -1 || rule.getBrandId().longValue() == brandId.longValue()) {
                    return rule;
                }
            }
        }
        // LOGGER.info("Could not get any rule for vendor {} product {}", vendorId, brandId);
        return null;
    }

    public Map<Long, Integer> getBoutiqueImgSortMap() {
        return boutiqueImgSortMap;
    }

    public Boolean getAggregateFlag() {
        return aggregateFlag;
    }

    public void setAggregateFlag(Boolean aggregateFlag) {
        this.aggregateFlag = aggregateFlag;
    }
}
